﻿/*************************************************************************************************************
  Copyright (c) 2010, Zack Evans
  All rights reserved.

  Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
* Neither the name of CapriSoft nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 
  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, 
  OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 
  OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
**************************************************************************************************************/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Common;
using System.Data;
using Extention;

namespace DataAccess
{
    public class SqlCommand : ICommand, IDbCommand, ICloneable
    {
        private System.Data.SqlClient.SqlCommand _innerCommand = null;

        public SqlCommand()
            : this(string.Empty)
        {
        }

        public SqlCommand(string cmdText)
            : this(cmdText, default(SqlConnection))
        {
        }

        public SqlCommand(string cmdText, SqlConnection connection)
        {
             this._innerCommand =
                        new System.Data.SqlClient.SqlCommand(cmdText, (System.Data.SqlClient.SqlConnection)connection);
        }

        public SqlCommand(string cmdText, SqlConnection connection, System.Data.SqlClient.SqlTransaction transaction)
        {
             this._innerCommand =
                        new System.Data.SqlClient.SqlCommand(cmdText, (System.Data.SqlClient.SqlConnection)connection, transaction);
        }

        public object Clone()
        {
            /*if (this._innerCommand != null && !String.IsNullOrEmpty(this._innerConnection.ConnectionString))
                return new SqlCommand(this._innerConnection.ConnectionString);
            else if (this._innerConnection != null && String.IsNullOrEmpty(this._innerConnection.ConnectionString))
                return new SqlConnection();
            else if (this._innerConnection == null)
                throw new ApplicationException("cannot clone disposed connection");*/

            return default(object);
        }
        public void Dispose()
        {
            if (this._innerCommand != null)
                this._innerCommand.Dispose();
        }
        public void Cancel()
        {
           if (this._innerCommand != null)
               this._innerCommand.Cancel();
        }
        public string CommandText
        {
            get
            {
                if (this._innerCommand != null)
                    return this._innerCommand.CommandText;
                else throw new ApplicationException("could not return command text");
            }
            set
            {
                if (this._innerCommand != null)
                    this._innerCommand.CommandText = value;
                else throw new ApplicationException("could not set command text");
            }
        }
        public int CommandTimeout
        {
            get
            {
                if (this._innerCommand != null)
                    return this._innerCommand.CommandTimeout;
                else throw new ApplicationException("could not return command timeout");
            }
            set
            {
                if (this._innerCommand != null)
                    this._innerCommand.CommandTimeout = value;
                else throw new ApplicationException("could not set command timeout");
            }
        }
        public CommandType CommandType
        {
            get
            {
                if (this._innerCommand != null)
                    return this._innerCommand.CommandType;
                else throw new ApplicationException("could not return command type");
            }
            set
            {
                if (this._innerCommand != null)
                    this._innerCommand.CommandType = value;
                else throw new ApplicationException("could not set command type");
            }
        }
        public IDbConnection Connection
        {
            get
            {
                if (this._innerCommand != null)
                    return this._innerCommand.Connection;
                else throw new ApplicationException("could not return command connection");
            }
            set
            {
                if (this._innerCommand != null)
                    this._innerCommand.Connection = (System.Data.SqlClient.SqlConnection)value;
                else throw new ApplicationException("could not set command connection");
            }
        }
        public IDbDataParameter CreateParameter()
        {
            if (this._innerCommand != null)
                return this._innerCommand.CreateParameter();
            else return default(IDbDataParameter);
        }
        public int ExecuteNonQuery()
        {
            if (this._innerCommand != null)
                return this._innerCommand.ExecuteNonQuery();
            else return default(int);
        }
        public IDataReader ExecuteReader(CommandBehavior behavior)
        {
            //this is to demonstrate the honoring of the IDataReader interface
            if (this._innerCommand != null)
            {
                ListEx<GenericDataObject> readerResult =
                    new ListEx<GenericDataObject>(this._innerCommand.ExecuteReader(behavior));
               
                return readerResult;
            }
            else return default(IDataReader);
        }
        public IDataReader ExecuteReader()
        {
            //this is to demonstrate by honoring the IDataReader interface
            if (this._innerCommand != null)
            {
                ListEx<GenericDataObject> readerResult =
                    new ListEx<GenericDataObject>(this._innerCommand.ExecuteReader());
                
                return readerResult;
            }
            else return default(IDataReader);
        }
        public object ExecuteScalar()
        {
            if (this._innerCommand != null)
                return this._innerCommand.ExecuteScalar();
            else return default(object);
        }
        public IDataParameterCollection Parameters
        {
            get
            {
                if (this._innerCommand != null)
                    return this._innerCommand.Parameters;
                else throw new ApplicationException("could not return command parameters");
            }
        }
        public void Prepare()
        {
            if (this._innerCommand != null)
                this._innerCommand.Prepare();
        }
        public IDbTransaction Transaction
        {
            get
            {
                if (this._innerCommand != null)
                    return this._innerCommand.Transaction;
                else throw new ApplicationException("could not return command transaction");
            }
            set
            {
                if (this._innerCommand != null)
                    this._innerCommand.Transaction = (System.Data.SqlClient.SqlTransaction)value;
                else throw new ApplicationException("could not set command transaction");
            }
        }
        public UpdateRowSource UpdatedRowSource
        {
            get
            {
                if (this._innerCommand != null)
                    return this._innerCommand.UpdatedRowSource;
                else throw new ApplicationException("could not return command updated row source");
            }
            set
            {
                if (this._innerCommand != null)
                    this._innerCommand.UpdatedRowSource = value;
                else throw new ApplicationException("could not set command updated row source");
            }
        }

        public static explicit operator System.Data.SqlClient.SqlCommand(SqlCommand custom)
        {
            return custom._innerCommand;
        }
    }
}